package com.paimeng.Internetworm;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.Random;

import static com.paimeng.Internetworm.NewsDAO.insert;

/**
 * @author: 潘辉
 * @Title: HtmlUrls
 * @ProjectName: paimeng
 * @Description: 暂无描述
 * @date: 2021/6/8 15:39
 */
public class HtmlUrls {
    public static void main(String[] args) throws SQLException, IOException {

        geturllist();
    }

    public static void geturllist() throws IOException, SQLException {
        //获取请求  https://news.baidu.com/guonei

        for (int i = 1; i <= 4; i++) {
            String url = "https://search.bilibili.com/article?keyword=%E5%8E%9F%E7%A5%9E%E4%BA%89%E8%AE%AE&from_source=web_search&page=" + i;

            //解析网页 Jsoup返回的document就是浏览器的document页面对象
            Document document = Jsoup.parse(new URL(url), 30000);
            //所有你在js对象中使用的办法你在这里都可以用
            Elements els = document.select("li.article-item");

            Random random = new Random(1);
            for (Element element : els) {
                String li = element.getElementsByTag("a").eq(0).attr("href");
                String title = element.getElementsByTag("a").eq(0).attr("title");


                int hotindex = random.nextInt(1000000);
                NewsUrl newsUrl = new NewsUrl();
                newsUrl.setUrl("https:" + li);
                newsUrl.setTitle(title);
                newsUrl.setHotindex(hotindex);
                insert(newsUrl);

            }
        }
    }



}
